package com.ftg.learn.chapter13.newmodel;

import java.util.Arrays;

/**
 * @author kangjx
 * @createTime 2022/8/8
 */
public abstract class EntryTable {

    private Entry[] entries;

    private int index = 0;

    private final int  default_capcity =10;

    public EntryTable(){
        entries = new Entry[default_capcity];
    }

    public EntryTable(int capcity){
        entries = new Entry[capcity];
    }

    public void add(Entry e){
        //容错
        //扩容
        entries[this.index++] = e;
    }

    public void add(Entry... e){
        //容错
        //扩容
        for (Entry entry : e) {
            this.add(entry);
        }
    }

    public int size(){
        return this.index;
    }

    public Entry[] getAllEntries(){
        return Arrays.copyOf(entries,this.size());
    }

    public abstract void init(Entry[] entry);


    protected String findWord(String word,DicType dictype){
        if(dictype == DicType.chinatoenglish){
            for (int i = 0; i < this.getAllEntries().length; i++) {
                if(this.getAllEntries()[i].getZhName().equals(word)){
                    return this.getAllEntries()[i].getEnName();
                }
            }
        }else if(dictype == DicType.englishtochina){
            for (int i = 0; i < this.getAllEntries().length; i++) {
                if(this.getAllEntries()[i].getEnName().equals(word)){
                    return this.getAllEntries()[i].getZhName();
                }
            }
        }
        return "没有";
    }
}
